// pages/search/search.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        array: ['品牌名称', ' 商品名称'],
        index: 1,
        fication_name: '',
        goods_name: '',
        inputValue: '',
        list: [],
        showList: [],
        indicatorDots: false,
        vertical: false,
        autoplay: false,
        interval: 2000,
        duration: 500,
        userInfo: '',
        ficationId: '',
        openId: '',
        isIphoneX: '',
        likeFlag: false,
        videoFlag: false,
        swiperCurrent: 0,
        searchArray: ['公司品牌', '公司名称'],
        searchIndex: 0,
        isShow2: false,
        goodsId_Arr: []
    },
    back(data) {
        var temp = {
            data: data
        }
        wx.redirectTo({
            url: `/pages/index/index`
        })
    },

    getListArr() {
        var _this = this;
        _this.data.fication_name = '';
        _this.data.goods_name = '';

        if (_this.data.index == 0) {
            _this.data.fication_name = _this.data.inputValue
        } else if (_this.data.index == 1) {
            _this.data.goods_name = _this.data.inputValue
        }
        wx.request({
            url: app.globalData.url+'/v1/goods/list',
            method: 'GET',
            data: {
                pagesize: '9999',
                current: '1',
                fication_name: _this.data.fication_name,
                goods_name: _this.data.goods_name,
                openid: app.globalData.openId
            },
            success: function(r) {
                if (r.data.code === 1) {
                    wx.showToast({
                        title: '没有搜索到数据',
                        icon: 'none',
                        duration: 2000
                    })
                    return;
                } else {
                    const temp = r.data.data.items.map((item, index) => {
                        if (index == 0 || index == 1) {
                            item.isShow = true;
                        } else {
                            item.isShow = false;
                        }
						item.gprice = Math.ceil(item.gprice)
                        item.likeFormat = _this.likeFormat(item.like);
						
                        item.likeTag = r.data.data.like.some(res => {
                            return res.detail_id === item.cid;
                        });
                        return item
                    })

                    app.globalData.ficationLOGO = temp[0].lurl

                    _this.setData({
                        goodsId_Arr: [temp[0].cid],
                        list: temp,
                        isShow2: true
                    })

                    wx.hideToast()
                }
                // _this.back(res.data.data)
            }
        })
    },

    bindPickerChange: function(e) {
        this.setData({
            index: e.detail.value
        })
    },

    bindKeyInput: function(e) {
        var _this = this;
        _this.setData({
            inputValue: e.detail.value
        })
    },

    like: function(e) {
        var _this = this;

        var temp = _this.data.list;
        const cid = _this.data.list[e.currentTarget.dataset.id].cid;

        console.log('_this.data.likeflag', temp[e.currentTarget.dataset.id].likeTag)
        if (!temp[e.currentTarget.dataset.id].likeTag) {
            temp[e.currentTarget.dataset.id].like++;
            wx.request({
                url: app.globalData.url+'/v1/goods/like',
                method: 'POST',
                data: {
                    cid: cid,
                    openid: app.globalData.openId
                },
                success: function(r) {
                    console.log(r);
                }
            })
            temp[e.currentTarget.dataset.id].likeFormat = _this.likeFormat(temp[e.currentTarget.dataset.id].like);
            temp[e.currentTarget.dataset.id].likeTag = true;
        } else {
            temp[e.currentTarget.dataset.id].like--;
            wx.request({
                url: app.globalData.url+'/v1/goods/cancel',
                method: 'POST',
                data: {
                    cid: cid,
                    openid: app.globalData.openId
                },
                success: function(r) {
                    console.log(r);
                }
            })
            temp[e.currentTarget.dataset.id].likeFormat = _this.likeFormat(temp[e.currentTarget.dataset.id].like);
            temp[e.currentTarget.dataset.id].likeTag = false;
        }

        this.setData({
            list: temp
        })
    },
    likeFormat(number) {
        if (number < 99999 && number > 9999) {
            return number.toString().slice(0, 1) + '.' + number.toString().slice(1, 2) + 'W';
        } else if (number < 999999 && number > 99999) {
            return number.toString().slice(0, 2) + '.' + number.toString().slice(2, 4) + 'W';
        } else if (number < 9999999 && number > 999999) {
            return number.toString().slice(0, 3) + '.' + number.toString().slice(3, 4) + 'W';
        } else if (number < 99999999 && number > 9999999) {
            return number.toString().slice(0, 4) + '.' + number.toString().slice(4, 5) + 'W';
        } else if (number < 999999999 && number > 99999999) {
            return number.toString().slice(0, 5) + '.' + number.toString().slice(5, 6) + 'W';
        } else if (number < 9999999999 && number > 999999999) {
            return number.toString().slice(0, 6) + '.' + number.toString().slice(6, 7) + 'W';
        }
        return number;
    },
    search() {
        wx.navigateTo({
            url: '/pages/search/search'
        })
    },
    swiperChange: function(e) {
        var idx = Number(e.detail.currentItemId)
        var v
        this.setData({
            videoFlag: false,
        })
        app.globalData.ficationLOGO = this.data.list[idx].lurl
        for (let i = 0; i < this.data.list.length; i++) {
            v = wx.createVideoContext('v' + i)
            v.seek(0)
            v.pause()
        }

        if (this.data.goodsId_Arr.indexOf(this.data.list[idx].cid) === -1) {
            const temp = this.data.goodsId_Arr;
            temp.push(this.data.list[idx].cid);
            this.setData({
                goodsId_Arr: temp
            })
        }
        // wx.createVideoContext('v' + idx).play()
    },

    goodSee() {
        var _this = this;
        console.log(_this.data.goodsId_Arr);
        wx.request({
            url: app.globalData.url+'/v1/goods/see',
            method: 'POST',
            data: {
                cid: _this.data.goodsId_Arr.join(),
                openid: app.globalData.openId
            },
            success: function(r) {
                console.log(r);
                if (r.data.code == 0)
                    _this.getList();
            }
        })
    },
    scrollEnd: function(e) {
        var idx = Number(e.detail.currentItemId)
        var list = this.data.list
        if (idx != 0 && idx != list.length - 1) {
            list.map(item => {
                item.isShow = false
                return item
            })
            list[idx].isShow = true
            list[idx + 1].isShow = true
            list[idx - 1].isShow = true
            this.setData({
                list: list,
            })
        }
        wx.createVideoContext('v' + idx).play()
    },
    videoClick: function(e) {
        let index = e.currentTarget.dataset.index
        let v = wx.createVideoContext('v' + index)
        if (this.data.videoFlag) {
            v.play()
            this.setData({
                videoFlag: false
            })
        } else {
            v.pause()
            this.setData({
                videoFlag: true
            })
        }
    },
    goPage: app.goPage,
    getUser: function(r) {
        console.log('r', r)
        var _this = this
        if (r.detail.errMsg == 'getUserInfo:ok') {
            app.globalData.userInfo = r.detail.userInfo
            this.setData({
                userInfo: r.detail.userInfo
            })
            wx.request({
                url: app.globalData.url+'/v1/wechat/user',
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                    nikname: r.detail.userInfo.nickName,
                    headimgurl: r.detail.userInfo.avatarUrl,
                    openid: app.globalData.openId
                },
                success: function(res) {
                    if (res.data.code == 0) {
                        wx.navigateTo({
                            url: '/pages/goodsDetail/goodsDetail?fid=' + r.currentTarget.dataset.fid
                        })
                    } else {
                        wx.showToast({
                            title: '获取信息失败',
                            icon: 'none',
                            duration: 2000
                        })
                    }
                }
            })
        }
    },
    getMobile: function() {
        var _this = this
        wx.request({
            url: app.globalData.url+'/v1/payment/show',
            method: 'GET',
            data: {
                openid: app.globalData.openId
            },
            success: function(r) {
                r = r.data
                if (r.code == 0) {
                    app.globalData.mobile = r.data.user.mobile
                } else {
                    wx.showToast({
                        title: r.data.message,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    },
    follow: function() {
        wx.showToast({
            title: '敬请期待',
            icon: 'none'
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
        console.log(this.data.goodsId_Arr);
        this.goodSee();
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        console.log(this.data.goodsId_Arr);
        this.goodSee();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})